package com.flow.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * String格式工具类
 * 
 * @author WangTeng
 * 
 */
public class TimeUtil {

    /**
     * 得到当前时间的String格式
     * 
     * @return 当前时间
     */
    public static final String getStringDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取平安日期
     * 
     * @param stringDate
     * @return
     */
    public static final String getPinganDate(String stringDate) {
        Date date = getDateFromString(stringDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    /**
     * 得到当前时间的yyyy-MM-dd HH:mm:ss格式
     * 
     * @param Date
     * @return 当前时间
     */
    public static final String getStringDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 得到当前时间的yyyy-MM-dd格式
     * 
     * @param Date
     * @return 当前时间
     */
    public static final String getShortStringDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static final Date getDateFromString(String stringDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(stringDate);
        } catch (ParseException e) {
            return new Date();
        }
    }

    /**
     * 得到时间字符串yyyy-MM-dd的毫秒数
     * 
     * @return Date
     */
    public static final long getDateMillFromYMDString(String stringDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(stringDate).getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }

    /**
     * 得到时间字符串的毫秒数
     * 
     * @return Date
     */
    public static final long getDateMillFromString(String stringDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(stringDate).getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }

    /**
     * 得到时间字符串的一年后毫秒数
     * 
     * @param stringDate
     * @return
     */
    public static final long getOneYearDateFromString(String stringDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(stringDate);
        } catch (ParseException e) {
            return 0L;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
        return cal.getTimeInMillis();
    }

    /**
     * 得到时间的N天N月后时间
     * 
     * @param date
     *            时间
     * @param day
     *            N天
     * @return
     */
    public static final Date getAfterMonthDateString(Date date, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (month != 0) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
        }
        if (day != 0) {
            cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + day);
        }
        return cal.getTime();
    }

    /**
     * 得到当前时间的String并取代分隔符并添加三位随机数
     * 
     * @return 时间+随机数
     */
    public static final String getUniqueNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String number = sdf.format(System.currentTimeMillis());
        int ran = new Random().nextInt(999) + 1;
        return number + supplementZero(String.valueOf(ran));
    }

    /**
     * 获取不重复的ID
     * 
     * @param seed
     *            数量
     * @return
     */
    public static final List<String> getUniqueNumber(int seed) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String number = sdf.format(System.currentTimeMillis());
        return getList(number, seed);
    }

    private static final List<String> getList(String str, int seed) {
        List<String> list = new ArrayList<String>(seed);
        for (int i = 1; i <= seed; i++) {
            int ran = new Random().nextInt(999) + 1;
            list.add(str + supplementZero(String.valueOf(ran)) + getZero(i));
        }
        return list;
    }

    private static final String getZero(int num) {
        if (num < 10) {
            return "00" + num;
        } else if (num >= 10 && num < 100) {
            return "0" + num;
        } else {
            return "" + num;
        }
    }

    /**
     * 补零
     * 
     * @param ran
     * @return
     */
    private static String supplementZero(String ran) {
        switch (ran.length()) {
        case 0:
            return "000";
        case 1:
            return ran + "00";
        case 2:
            return ran + "0";
        default:
            return ran;
        }
    }

    /**
     * 根据产品上架时间、筹款期限、发行周期获取收益时间Map<String, List>
     * 
     * @param sjsj
     *            上架时间
     * @param ckqx
     *            筹款期限
     * @param period
     *            发行周期
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static final Map<String, List> getIncomeTimeMap(String sjsj, int ckqx, int period) {
        Date workTime = getWorkTime(sjsj, ckqx);
        List<String> timeString = new ArrayList<String>();
        List<Long> timeLong = new ArrayList<Long>();
        Map tmpMap = null;
        for (int i = 1; i <= period; i++) {
            tmpMap = dateStringAndMillByPriod(workTime, i);
            timeString.add(String.valueOf(tmpMap.get("date")));
            timeLong.add(Long.parseLong(String.valueOf(tmpMap.get("mill"))));
        }
        Map<String, List> map = new HashMap<String, List>();
        map.put("timeString", timeString);
        map.put("timeLong", timeLong);
        return map;
    }

    /**
     * 根据日期，期数，获取期数日期
     * 
     * @param date
     * @param period
     * @return
     */
    public static final Date getDateByPriod(Date date, int period) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONDAY) + period);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
        return cal.getTime();
    }

    /**
     * 根据时间和期数获取日期毫秒数
     * 
     * @param date
     * @param period
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static final Map dateStringAndMillByPriod(Date date, int period) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONDAY) + period);
        Map map = new HashMap();
        map.put("mill", cal.getTimeInMillis());
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("date", format.format(new Date(cal.getTimeInMillis())));
        return map;
    }

    /**
     * 根据上架时间、筹款期限获取起息时间
     * 
     * @param sjsj
     * @param ckqx
     * @return
     */
    private static final Date getWorkTime(String sjsj, int ckqx) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(sjsj);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + ckqx);
        return cal.getTime();
    }
    
    /**
     *  获取当前时间的时分秒毫秒的字符串形式
     * @return String	
     *
     */
    public  static final String getAllTime() {
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date date = new Date();
        String timeStr = "";
        try {
        	timeStr = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeStr;
    }
}
